package com.uottawa.library.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.uottawa.library.domain.Item;
import com.uottawa.library.domain.LoanPeriod;
import com.uottawa.library.domain.User;
import com.uottawa.library.service.ItemService;
import com.uottawa.library.service.LoanPeriodService;

/**
 * Servlet implementation class RenewalServlet
 */
@WebServlet(name="/RenewalServlet",urlPatterns ={"/addRenewal.do","/removeRenewal.do","/updateRenewal.do"})
public class RenewalServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private LoanPeriodService loanPeriodService = new LoanPeriodService();

       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RenewalServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		

		String loanPeriodDuration = request.getParameter("loanPeriodDuration");
		

		if( loanPeriodDuration == null  )
		{
			 request.setAttribute("error", "all fields are required");
        	 request.getRequestDispatcher("/renewal.jsp").forward(request, response);
        	 return;
		}
	
		String url = request.getServletPath();
		if (url.contains("update")) {
			String loanPeriodId = request.getParameter("loanPeriodId");
			
			LoanPeriod loanPeriod = new LoanPeriod(Integer.parseInt(loanPeriodId),
					Integer.parseInt(loanPeriodDuration));
			String result = loanPeriodService.updateLoanPeriod(loanPeriod);
			request.setAttribute("result", result);
			
			
			List<LoanPeriod> loanPeriods = loanPeriodService.getAllLoanPeriod();
			request.setAttribute("loanPeriods", loanPeriods);

		}
		else if(url.contains("add"))
		{
			User user = (User) request.getSession().getAttribute("user");
			LoanPeriod lp = new LoanPeriod();
			lp.setDuration(Integer.parseInt(loanPeriodDuration));
			String result = loanPeriodService.addPeriod(lp, user);
			request.setAttribute("result", result);	
			//
			List<LoanPeriod> loanPeriods = loanPeriodService.getAllLoanPeriod();
			request.setAttribute("loanPeriods", loanPeriods);
		}
		
		
    	request.getRequestDispatcher("/renewal.jsp").forward(request, response);

		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String url = request.getServletPath();

		
		if (url.contains("update")) {
			
			List<LoanPeriod> loanPeriods = loanPeriodService.getAllLoanPeriod();
			request.setAttribute("loanPeriods", loanPeriods);
		}
			else if (url.contains("add")) {
				
				List<LoanPeriod> loanPeriods = loanPeriodService.getAllLoanPeriod();
				request.setAttribute("loanPeriods", loanPeriods);
			
		} else if (url.contains("remove")) {
			String loanPerodId = request.getParameter("id");
			if (loanPerodId != null) {
				String result = loanPeriodService.removeLoanPeriod(Integer.parseInt(loanPerodId));
				request.setAttribute("result", result);
			}
			List<LoanPeriod> loanPeriods = loanPeriodService.getAllLoanPeriod();
			request.setAttribute("loanPeriods", loanPeriods);
		}

    	request.getRequestDispatcher("/renewal.jsp").forward(request, response);
		
	}

}
